package com.spider.admin.init;


import com.spider.admin.agent.AgentServer;
import com.spider.admin.agent.PublishWeb;
import com.spider.admin.agent.WebNginxConf;
import com.spider.admin.lua.LuaResourcesFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Component
@Slf4j
public class AppStartupRunner implements ApplicationRunner {

    @Autowired
    private CacheInitService cacheInitService;

    @Autowired
    private UiServer uiServer;

    @Autowired
    private LuaResourcesFile luaResourcesFile;

    @Autowired
    private WebNginxConf webNginxConf;

    @Autowired
    private PublishWeb publishWeb;

    @Autowired
    AgentServer agentServer;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("============================初始化系统开始==================================");
        //初始化权限等缓存数据
        cacheInitService.initCache();
        /****************************************Web管理容器********************************************/
        //发布管理Web容器Lua脚本
        luaResourcesFile.copyAdminWebLuaFolder();
        //发布管理Web容器配置
        webNginxConf.writeWebConf();
        //发布管理Web前端脚本
        publishWeb.publishWebPage();
        //启动管理Web容器
        uiServer.startUiServer();

        log.info("Web容器启动");

        /****************************************代理管理容器********************************************/
        //生成代理容器
        agentServer.createAgentServer();
        //发布代理容器Lua脚本
        luaResourcesFile.copyAgentLuaFolder();
        //发布代理容器配置
//        nginxConf.copyAgentConf();
        //启动代理服务
        agentServer.startAgentServer();
        log.info("代理服务启动");
        log.info("============================初始化系统完成==================================");

    }
}

